﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FileToBase64
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            label1.Text = "";

            richTextBox1.AllowDrop = true;
            richTextBox1.DragEnter += new DragEventHandler(Form1_DragEnter);
            richTextBox1.DragDrop += new DragEventHandler(Form1_DragDrop);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName != string.Empty)
            {
                ProcessFile(openFileDialog1.FileName);
            }
        }

        private void ProcessFile(string FilePath)
        {
            label1.Text = Path.GetFileName(FilePath);
            byte[] bytes = File.ReadAllBytes(FilePath);
            richTextBox1.Text = Convert.ToBase64String(bytes);
            richTextBox1.SelectAll();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
            bool first = true;
            foreach (string file in files)
            {
                if (first)
                {
                    ProcessFile(file);
                }
                first = false;
            }
        }
    }
}
